/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package org.zakariya.flyoutmenu;

import ohos.agp.utils.Color;

/**
 * 颜色工具类
 */
public class ColorUtils {


    /**
     * Set {@link ohos.agp.components.Component} alpha
     *
     * @param color the color  selected
     * @param alpha the alpha selected
     * @return color
     * @throws IllegalArgumentException alpha must be between 0 and 255.
     */
    public static int setAlphaComponent(int color, int alpha) {
        if (alpha >= 0 && alpha <= 255) {
            return color & 16777215 | alpha << 24;
        } else {
            throw new IllegalArgumentException("alpha must be between 0 and 255.");
        }
    }

    /**
     * hsl 转 color
     *
     * @param hsl hsl数组
     * @return int
     */
    public static int HSLToColor(double[] hsl) {
        double h = hsl[0];
        double s = hsl[1];
        double l = hsl[2];
        double c = (1.0F - Math.abs(2.0F * l - 1.0F)) * s;
        double m = l - 0.5F * c;
        double x = c * (1.0F - Math.abs(h / 60.0F % 2.0F - 1.0F));
        int hueSegment = (int) h / 60;
        long r = 0;
        long g = 0;
        long b = 0;
        switch (hueSegment) {
            case 0:
                r = Math.round(255.0F * (c + m));
                g = Math.round(255.0F * (x + m));
                b = Math.round(255.0F * m);
                break;
            case 1:
                r = Math.round(255.0F * (x + m));
                g = Math.round(255.0F * (c + m));
                b = Math.round(255.0F * m);
                break;
            case 2:
                r = Math.round(255.0F * m);
                g = Math.round(255.0F * (c + m));
                b = Math.round(255.0F * (x + m));
                break;
            case 3:
                r = Math.round(255.0F * m);
                g = Math.round(255.0F * (x + m));
                b = Math.round(255.0F * (c + m));
                break;
            case 4:
                r = Math.round(255.0F * (x + m));
                g = Math.round(255.0F * m);
                b = Math.round(255.0F * (c + m));
                break;
            case 5:
            case 6:
                r = Math.round(255.0F * (c + m));
                g = Math.round(255.0F * m);
                b = Math.round(255.0F * (x + m));
        }

        r = constrain(r, 0, 255);
        g = constrain(g, 0, 255);
        b = constrain(b, 0, 255);
        return Color.rgb((int)r, (int)g, (int)b);
    }

    private static long constrain(long amount, long low, long high) {
        return amount < low ? low : (amount > high ? high : amount);
    }


}
